<?php

namespace app\admin\model;

use think\Model;
use traits\model\SoftDelete;

class ShopAudit extends Model
{
    use SoftDelete;
    // 表名
    protected $name = 'user_shop_audit';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    protected $deleteTime = 'delete_time';

    // 追加属性
    protected $append = [
        'expiration_time_text',
        'apply_time_text',
        'audit_time_text',
    ];

    const STATUS_AUDIT_ING = 1; // 审核中
    const STATUS_AUDIT_PASS = 2; // 审核通过
    const STATUS_AUDIT_REFUSE = 3; // 审核拒绝
    const STATUS_AUDIT_EXPIRATION = 4; // 已过期
    const STATUS_AUDIT_HIDDEN = 5; // 已禁用

    const TYPE_SHOP_ONE = 1; // 单店
    const TYPE_SHOP_CHAIN = 2; // 连锁

    public static function getTypeArray(){
        return [
            self::TYPE_SHOP_ONE     => '单店',
            self::TYPE_SHOP_CHAIN   => '连锁',
        ];
    }
    public static function getStatusArray(){
        return [
            self::STATUS_AUDIT_ING     => '审核中',
            self::STATUS_AUDIT_PASS    => '审核通过',
            self::STATUS_AUDIT_REFUSE  => '审核拒绝',
        ];
    }
    protected static function init()
    {
    }

    public function getExpirationTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['expiration_time']) ? $data['expiration_time'] : '');
        return is_numeric($value) ? date("Y-m-d", $value) : $value;
    }

    public function getApplyTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['apply_time']) ? $data['apply_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    public function getAuditTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['audit_time']) ? $data['audit_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setExpirationTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime(date("Y-m-d  23:59:59", strtotime($value))) : $value);
    }

    protected function setApplyTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setAuditTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    public function user()
    {
        return $this->belongsTo('User', 'user_id', 'id')->setEagerlyType(0);
    }

}
